package pg

import (
	"fmt"
	"purchase-mall/models"
)

// InsertData 想数据库中插入一条新的用户记录
func InsertData(data *models.Aliyun) (err error) {
	//防止自增主键冲突，主要考虑有时候手动添加数据
	stmt2, _ := db.Prepare("SELECT setval('metric_peak_id_seq', (SELECT max(id) FROM \"metric_peak\"))")
	_, err = stmt2.Exec()
	if err != nil {
		fmt.Println("prevent insert repeat key（id） error: %v", err)
	}

	stmt, err := db.Prepare("INSERT INTO metric_peak(type_name,metric_name,peak_value,peak_time,max_value,created_at,stat_date,instance,host) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)")
	if err != nil {
		fmt.Printf("postgres stmt is err %s", err)
		return err
	}

	_, err = stmt.Exec(data.TypeName, data.MetricName, data.PeakValue, data.PeakTime, data.MaxValue, data.CreatedAt, data.StatDate, data.Instance, data.Host)
	//res, err := stmt.Exec(1, "kafka", 1, "内存", 88.88, "2006-01-02 15:04:05", 90.99, "2006-01-02 15:04:05")

	if err != nil {
		fmt.Printf("postgres  res is err %s", err)
		return err
	}

	return
}
